package com.dz.goods.service;

import com.dz.common.base.BasePageResultsVO;
import com.dz.common.constants.CodeDomain;
import com.dz.goods.domain.entity.Album;
import com.dz.goods.domain.entity.Category;
import com.dz.goods.domain.entity.QCategory;
import com.dz.goods.domain.repository.CategoryRepository;
import com.dz.goods.domain.vo.AlbumResultsVO;
import com.dz.goods.domain.vo.CategoryParamsVO;
import com.dz.goods.domain.vo.CategoryResultsVO;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName : CategoryServiceImpl
 * @Description :
 * @Author : ming
 * @Date: 2020-12-02 13:40
 **/
@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Override
    @Transactional
    public Category findById(Long id) {
        return categoryRepository.findById(id).get();
    }
    @Transactional
    @Override
    public Category add(Category category) {
        return categoryRepository.saveAndFlush(category);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        Category category = categoryRepository.findById(id).get();
        category.setDeleteFlag(Integer.valueOf(CodeDomain.DELETE_FLAG_YES.value()));
        categoryRepository.saveAndFlush(category);
    }
    @Transactional
    @Override
    public Category edit(Category category) {
        return categoryRepository.saveAndFlush(category);
    }
    @Transactional
    @Override
    public BasePageResultsVO<CategoryResultsVO> getCategoryList(CategoryParamsVO vo) {
        QCategory category=QCategory.category;
        BooleanBuilder where=new BooleanBuilder();
        if(StringUtils.isNotEmpty(vo.getId())){
            where.and(category.id.eq(Long.parseLong(vo.getId())));
        }
        if(StringUtils.isNotEmpty(vo.getName())){
            where.and(category.name.eq(vo.getName()));
        }
        if(StringUtils.isNotEmpty(vo.getGoodsNum())){
            where.and(category.goodsNum.eq(Long.valueOf(vo.getGoodsNum())));
        }
        if(StringUtils.isNotEmpty(vo.getShowFlag())){
            where.and(category.showFlag.eq(vo.getShowFlag()));
        }
        if(StringUtils.isNotEmpty(vo.getMenuFlag())){
            where.and(category.menuFlag.eq(vo.getMenuFlag()));
        }
        if(StringUtils.isNotEmpty(vo.getParentId())){
            where.and(category.parentId.eq(Long.valueOf(vo.getParentId())));
        }
        if(StringUtils.isNotEmpty(vo.getTemplateId())){
            where.and(category.templateId.eq(Long.valueOf(vo.getTemplateId())));
        }
        where.and(category.deleteFlag.eq(Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value())));
        JPAQuery<Category> query=jpaQueryFactory.select(category).from(category).where(where.getValue());
        QueryResults<Category> fetchResults = query.offset((vo.getPageNum() - 1L) * vo.getPageSize()).limit(vo.getPageSize()).fetchResults();
        List<CategoryResultsVO> list = fetchResults.getResults().stream().map(e -> CategoryResultsVO.builder().id(e.getId())
                .name(e.getName())
                .goodsNum(e.getGoodsNum())
                .menuFlag(e.getMenuFlag())
                .showFlag(e.getShowFlag())
                .parentId(e.getParentId())
                .templateId(e.getTemplateId())
                .build()).collect(Collectors.toList());
        return BasePageResultsVO.success(vo.getPageNum(),vo.getPageSize(),fetchResults.getTotal(),list);
    }
}
